//
//  HXAllGroupPeopleCollectionView.m
//  KD
//
//  Created by GaoFei on 16/7/8.
//  Copyright © 2016年 Whx. All rights reserved.
//

#define kCellClearance 10
#import "HXAllGroupPeopleCollectionView.h"
#import "HXAllGroupPeopleCollectionViewCell.h"

@interface HXAllGroupPeopleCollectionView ()

@end

static NSString * identifierCell = @"AllGroupPeopleCell";
static NSString * identifierHeader = @"identifierHeader";

@implementation HXAllGroupPeopleCollectionView

/**
 设置页面的布局
 */
-(UICollectionViewFlowLayout *)loadCreateFlowLayout{

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置垂直与水平的最小间隙
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    return layout;
}

-(instancetype)initWithFrame:(CGRect)frame{

    
    self = [super initWithFrame:frame collectionViewLayout:[self loadCreateFlowLayout]];
    
    if (self) {
    
        //设置数据源和代理协议
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [self registerClass:[HXAllGroupPeopleCollectionViewCell class] forCellWithReuseIdentifier:identifierCell];
        
        //注册头视图
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierHeader];
    }
    
    return self;
}

/**
 重写数据源数组的set方法
 */
-(void)setDataArr:(NSArray *)dataArr{

    _dataArr = dataArr;
    
    [self reloadData];
}

#pragma mark --------- // UICollectionViewDataSource \\
//返回单元格组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _dataArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HXAllGroupPeopleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    
    return cell;
}

//设置单元格的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake((HXSCREEN_WIDTH - 5*kCellClearance)/4, (HXSCREEN_WIDTH - 5*kCellClearance)/4);
}


//距离四周的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    
    return UIEdgeInsetsMake(kCellClearance, kCellClearance, kCellClearance, kCellClearance);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(HXSCREEN_WIDTH, 44);
}

//注册头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:identifierHeader forIndexPath:indexPath];
        
        headerView.backgroundColor = [UIColor lightGrayColor];
        
        UILabel * itemNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 6, HXSCREEN_WIDTH, 32)];
        itemNameLabel.text = [NSString stringWithFormat:@"全体群成员(%ld)",_dataArr.count];
        itemNameLabel.contentMode = UIViewContentModeLeft;
        [headerView addSubview:itemNameLabel];
        
        return headerView;
    }
    
    return nil;
}

//单元格的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
